<?php
class User_Service_User
{
	public function __construct(){
		
	}

	public static function add($row){
		$Obj = new User_Model_User();
		return $Obj->add($row);		
	}
	
	public static function update($row,$user_id){
		$Obj = new User_Model_User();
		return $Obj->update($row,$user_id);		
	}
	
	public static function delete($id){
		$Obj = new User_Model_User();
		return $Obj->delete($id);		
	}
	
	public static function getList($options,$type='*',$order='user_id', $page=0, $pageSize=0){
		$obj = new User_Model_User();	
		$start = 0;
		if($pageSize>0 and $page>0){
			$start =($page-1)*$pageSize;			
		}
		$userList = $obj->getList($options,$type,$order, $start, $pageSize);
		if($type=='*'){
			foreach($userList as $key=>$val){
				 $department = User_Service_Department::getById($val['ud_id']);
				 $userList[$key]['department'] = $department['ud_name'];
				 $position = User_Service_Position::getByUpid($val['up_id']); 
				 $userList[$key]['position'] = $position['up_name']; 
			}
		}
		return	$userList;	
	}	
	 
	public static function getByUserId($userId){		
		$obj = new User_Model_User();
		$row=$obj->getByUserId($userId);
		if(!empty($row)){
			$row=$row->toArray();
		}
		return $row;
	}
	
	public static function getByLoginId($Id){		
		$obj = new User_Model_User();
		$row=$obj->getByLoginId($Id);
		if(!empty($row)){
			$row=$row->toArray();
		}
		return $row;
	}
	
	public static function comparePassword($pwd1,$pwd2){		
		if($pwd1==$pwd2){
			return true;
		}else {
			return false;
		}
	}
	
	public static function getRealIp(){
       if(getenv('HTTP_CLIENT_IP')){
         $ip=getenv('HTTP_CLIENT_IP');
       }
       elseif(getenv('HTTP_X_FORWARDED_FOR')){
         $ip=getenv('HTTP_X_FORWARDED_FOR');
      }
      elseif(getenv('REMOTE_ADDR')){
         $ip=getenv('REMOTE_ADDR');
      }
      else{
         $ip=$HTTP_SERVER_VARS['REMOTE_ADDR'];
      }
         return $ip;
   }
   
   public static function getByCond($cond,$type,$order,$count,$offset){
   	$Obj = new User_Model_User();
   	return $Obj->getByCond($cond,$type,$order,$count,$offset);
   }
   
   public static function getByfield($field, $value) {
   	$obj = new User_Model_User();
   	$rows = $obj->getByfield($field, $value);
   	if (!empty ($rows)) {
   		$rows = $rows->toArray();
   	}
   	return $rows;
   }

   
}